//
//  ContentViewController.m
//  cardmenNew
//
//  Created by Dengzeyu on 16/1/4.
//  Copyright © 2016年 Dengzeyu. All rights reserved.
//

#import "ContentViewController.h"
#import "jingtai.h"
#import "UIImage_imageScale.h"
#import "UMCommunity.h"
#import "UMComUser.h"
#import "UMComSession.h"
#import "UMComAction.h"
#import "GetURLContent.h"
#import "Model.h"
#import "MyView.h"
#import "MHFileTool.h"
#import "M_content.h"
@implementation ContentViewController
- (IBAction)goback:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
//
- (BOOL)prefersStatusBarHidden//for iOS7.0
{
    return YES;
}
-(void)viewDidLoad
{
    switch (self.leixing) {
        case 0:
        {
            [self setNeedsStatusBarAppearanceUpdate];//self UIViewController实例
            [jingtai sharedSingleton].contentview =self;
            [self.liebiao setBackgroundImage:[UIImage imageNamed:@"liebiao_on.png"] forState:UIControlStateHighlighted];
            NSString * url = [NSString stringWithFormat:@"%@/cardmennew/getkindID.aspx?kind=%@&shop=%@",[jingtai sharedSingleton].mainIP,self.kindname,self.shopname];
            NSString * kindID = [[[GetURLContent alloc] init] getkindID:url];
            self.kindID=kindID;
            self.appList = [[NSMutableArray alloc] init];
            self.localapplist =[[NSMutableArray alloc] init];
            self.URLapplist = [[NSMutableArray alloc]init];
            UIImageView * imagebackground = [[UIImageView alloc] initWithFrame:[jingtai sharedSingleton].kindview.tabBarController.tabBar.frame];
            UIImage * barimage = [UIImage imageNamed:@"table_pic"];
            [barimage TransformtoSize:CGSizeMake(self.view.frame.size.width, [jingtai sharedSingleton].kindview.tabBarController.tabBar.frame.size.height)];
            [imagebackground setImage:barimage];
            self.guanzhuzhanghao = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/2.0, imagebackground.frame.size.height) ];
            [self.guanzhuzhanghao setTitle:@"关注账号" forState:UIControlStateNormal];
            self.guanzhuzhanghao.tag =0;
            self.dianzanButton =[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2.0, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/2.0, imagebackground.frame.size.height) ];
            [ self.dianzanButton setTitle:@"点赞" forState:UIControlStateNormal];
            [ self.dianzanButton addTarget:self action:@selector(dianzan:) forControlEvents:UIControlEventTouchUpInside];
            [self.guanzhuzhanghao addTarget:self action:@selector(guanzhu:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:imagebackground];
            [self.view addSubview:self.guanzhuzhanghao];
            [self.view addSubview: self.dianzanButton];
            NSString * urldianzan = [NSString stringWithFormat:@"%@/cardmennew/dianzan.aspx?kindid=%@&comid=%@&isdianzan=NO",[jingtai sharedSingleton].mainIP,self.kindID,[UMComSession sharedInstance].uid];
            
            NSString * strdianza = [[[GetURLContent alloc] init] getisdianzan:urldianzan];
            if([strdianza isEqualToString:@"hasdianzan"])
            {
                [self.dianzanButton setEnabled:NO];
                [self.dianzanButton setTitle:@"已经点赞" forState:UIControlStateNormal];
            }
            [self isGuanzhuzhanghao];
            GetURLContent * getcontent = [[GetURLContent alloc] init];
            getcontent.shopname = self.shopname;
            getcontent.kindname = self.kindname;
            NSString * urlcontent = [NSString stringWithFormat:@"%@/cardmennew/getcontent.aspx?kind=%@&shop=%@&unity=%@",[jingtai sharedSingleton].mainIP,self.kindname,self.shopname,[jingtai sharedSingleton].unityversion];
            [getcontent Load:urlcontent andkind:@"content"];
            self.isdelete =false;

        }
            break;
            case 1:
        {
            [self setNeedsStatusBarAppearanceUpdate];//self UIViewController实例
            [jingtai sharedSingleton].contentview =self;
            [self.liebiao setBackgroundImage:[UIImage imageNamed:@"liebiao_on.png"] forState:UIControlStateHighlighted];
            NSString * url = [NSString stringWithFormat:@"%@/cardmennew/getkindID.aspx?kind=%@&shop=%@",[jingtai sharedSingleton].mainIP,self.kindname,self.shopname];
            NSString * kindID = [[[GetURLContent alloc] init] getkindID:url];
            self.kindID=kindID;
            self.appList = [[NSMutableArray alloc] init];
            self.localapplist =[[NSMutableArray alloc] init];
            self.URLapplist = [[NSMutableArray alloc]init];
            UIImageView * imagebackground = [[UIImageView alloc] initWithFrame:[jingtai sharedSingleton].kindview.tabBarController.tabBar.frame];
            UIImage * barimage = [UIImage imageNamed:@"table_pic"];
            [barimage TransformtoSize:CGSizeMake(self.view.frame.size.width, [jingtai sharedSingleton].kindview.tabBarController.tabBar.frame.size.height)];
            [imagebackground setImage:barimage];
            self.allButton =[[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width/5.0)*0, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/5.0, imagebackground.frame.size.height) ];
            [self.allButton setTitle:@"所有" forState:UIControlStateNormal];
            self.CardButton = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width/5.0)*1, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/5.0, imagebackground.frame.size.height) ];
            [self.CardButton setTitle:@"卡片" forState:UIControlStateNormal];
            self.FillButton =  [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width/5.0)*2, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/5.0, imagebackground.frame.size.height) ];
            [self.FillButton setTitle:@"绘画" forState:UIControlStateNormal];
              self.guantuButton =  [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width/5.0)*3, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/5.0, imagebackground.frame.size.height) ];
            [self.guantuButton setTitle:@"挂图" forState:UIControlStateNormal];
            self.PenButton =[[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width/5.0)*4, self.view.frame.size.height-imagebackground.frame.size.height, self.view.frame.size.width/5.0, imagebackground.frame.size.height) ];
            [self.PenButton setTitle:@"笔" forState:UIControlStateNormal];
            [self.allButton addTarget:self action:@selector(allbutton) forControlEvents:UIControlEventTouchUpInside];
            [self.CardButton addTarget:self action:@selector(cardButton) forControlEvents:UIControlEventTouchUpInside];
            [self.FillButton addTarget:self action:@selector(fillButton) forControlEvents:UIControlEventTouchUpInside];
            [self.guantuButton addTarget:self action:@selector(guatubutton) forControlEvents:UIControlEventTouchUpInside];
            [self.PenButton addTarget:self action:@selector(penButton) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:imagebackground];

            [self.view addSubview:self.allButton];
            [self.view addSubview:self.CardButton];
            [self.view addSubview:self.FillButton];
            [self.view addSubview:self.guantuButton];
            [self.view addSubview:self.PenButton];
            self.isdelete =false;
            [self selectappList:@"all" andisdelete:NO];
            [self loadscroll];
            self.currentKind = @"all";
        }
            break;
        default:
            break;
    }
    
}
-(void)allbutton
{
    [self selectappList:@"all" andisdelete:NO];
    [self loadscroll];
     self.currentKind = @"all";
}
-(void)cardButton
{
    [self selectappList:@"card" andisdelete:NO];
     [self loadscroll];
     self.currentKind = @"card";
}
-(void)fillButton
{
    [self selectappList:@"fill" andisdelete:NO];
     [self loadscroll];
    self.currentKind = @"fill";
    
}
-(void)guatubutton
{
    [self selectappList:@"guatu" andisdelete:NO];
     [self loadscroll];
     self.currentKind = @"guatu";
}
-(void)penButton
{
    [self selectappList:@"pen" andisdelete:NO];
     [self loadscroll];
     self.currentKind = @"pen";
}
- (IBAction)shanchu:(id)sender {
    if(!self.isdelete)
    {
        [self.shanchu setImage:[UIImage imageNamed:@"delete_on.png"] forState:UIControlStateNormal];
        if(self.leixing==0)
        {
        [self loadscroll:YES];
        }
        else if(self.leixing == 1)
        {
            [self  selectappList:self.currentKind andisdelete:YES];
            [self loadscroll];
        }
        self.isdelete =YES;
    }
    else
    {
        [self.shanchu setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
        if(self.leixing==0)
        {
            [self loadscroll:YES];
        }
        else if(self.leixing == 1)
        {
            [self  selectappList:self.currentKind andisdelete:NO];
             [self loadscroll];
        }

       self.isdelete =NO;
    }
    
    
}
-(void)isGuanzhuzhanghao
{
    if([UMComSession sharedInstance].uid)
    {
        UMComFollowersRequest * UMcomfellow = [[UMComFollowersRequest alloc] initWithUid:[UMComSession sharedInstance].uid count:NSIntegerMax];
        [UMcomfellow fetchRequestFromServer:^(NSArray *data, BOOL haveNextPage, NSError *error) {
            NSLog(@"feeds is %@",data);
            if(error != nil)
            {
                NSLog(@"error  %@",error.description);
            }
            NSLog(@"关注用户数目 %d",data.count);
            BOOL templock=YES;
           
            for(int i =0;i<data.count;i++)
            {
                UMComUser * user =[data objectAtIndex:i];
                NSLog(@"comuserid %@  AND  self.userid: %@ ",user.uid,self.userid);
                if([user.uid isEqualToString: self.userid])
                {
                    self.isguanzhu = YES;
                    [self updateguanzhuButton];
                    templock =NO;
                    break;
                }
            }
            if(templock)
             [self updateguanzhuButton];


        }];
    
    }
    
    else
    {
        [self updateguanzhuButton];
    }
}
-(void) updateguanzhuButton
{
    if(self.isguanzhu)
    {
       
               [self.guanzhuzhanghao setTitle:@"取消关注" forState:UIControlStateNormal];
        [self.guanzhuzhanghao setTag:1];
    }
    else
    {
        [self.guanzhuzhanghao setTitle:@"关注账号" forState:UIControlStateNormal];
        [self.guanzhuzhanghao setTag:0];
    }
}
-(void)selectappList:(NSString *)kind andisdelete:(BOOL)isdelete

{
    [self.appList removeAllObjects];
    if([kind isEqualToString:@"all"])
    {
        NSMutableArray * downloaded =[[NSMutableArray alloc] init];
        NSFileManager * fm = [NSFileManager defaultManager];
        NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
        if([fm fileExistsAtPath:download])
        {
            self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
        }
        for (NSDictionary *temp in self.localapplist) {
            
            M_content * content = [[M_content alloc] init];
            [content setself:temp];
            if(isdelete)
            content.state =3; //删除状态！
            else
                content.state =1;
            [downloaded addObject:content];
            
        }
        self.appList = [downloaded mutableCopy];
    }
    else if([kind isEqualToString:@"card"])
    {
        NSMutableArray * downloaded =[[NSMutableArray alloc] init];
        NSFileManager * fm = [NSFileManager defaultManager];
        NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
        if([fm fileExistsAtPath:download])
        {
            self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
        }
        for (NSDictionary *temp in self.localapplist) {
            
            M_content * content = [[M_content alloc] init];
  
            if([[temp objectForKey:@"kindname"] isEqualToString:@"卡片"])
            {
            [content setself:temp];
                if(isdelete)
                    content.state =3; //删除状态！
                else
                    content.state =1;
            [downloaded addObject:content];
            }
        }
        self.appList = [downloaded mutableCopy];
    }
    else if([kind isEqualToString:@"fill"])
    {
        NSMutableArray * downloaded =[[NSMutableArray alloc] init];
        NSFileManager * fm = [NSFileManager defaultManager];
        NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
        if([fm fileExistsAtPath:download])
        {
            self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
        }
        for (NSDictionary *temp in self.localapplist) {
            
            M_content * content = [[M_content alloc] init];
            
            if([[temp objectForKey:@"kindname"] isEqualToString:@"绘本"])
            {
                [content setself:temp];
                if(isdelete)
                    content.state =3; //删除状态！
                else
                    content.state =1;
                [downloaded addObject:content];
            }
        }
        self.appList = [downloaded mutableCopy];
    }
    else if([kind isEqualToString:@"guatu"])
    {
        NSMutableArray * downloaded =[[NSMutableArray alloc] init];
        NSFileManager * fm = [NSFileManager defaultManager];
        NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
        if([fm fileExistsAtPath:download])
        {
            self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
        }
        for (NSDictionary *temp in self.localapplist) {
            
            M_content * content = [[M_content alloc] init];
            
            if([[temp objectForKey:@"kindname"] isEqualToString:@"挂图"])
            {
                [content setself:temp];
                if(isdelete)
                    content.state =3; //删除状态！
                else
                    content.state =1;
                [downloaded addObject:content];
            }
        }
        self.appList = [downloaded mutableCopy];
    }
    else if([kind isEqualToString:@"pen"])
    {
        NSMutableArray * downloaded =[[NSMutableArray alloc] init];
        NSFileManager * fm = [NSFileManager defaultManager];
        NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
        if([fm fileExistsAtPath:download])
        {
            self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
        }
        for (NSDictionary *temp in self.localapplist) {
            
            M_content * content = [[M_content alloc] init];
            
            if([[temp objectForKey:@"kindname"] isEqualToString:@"笔"])
            {
                [content setself:temp];
                if(isdelete)
                    content.state =3; //删除状态！
                else
                    content.state =1;
                [downloaded addObject:content];
            }
        }
        self.appList = [downloaded mutableCopy];
    }
}
-(void)updateapplist:(BOOL)isdelete
{
        [self.appList removeAllObjects];
    if(isdelete)
    {
        NSMutableArray * downloaded =[[NSMutableArray alloc] init];
        NSFileManager * fm = [NSFileManager defaultManager];
        NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
        if([fm fileExistsAtPath:download])
        {
            self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
        }
        for (NSDictionary *temp in self.localapplist) {
       
                        M_content * content = [[M_content alloc] init];
                        [content setself:temp];
                        content.state =3; //删除状态！
                        [downloaded addObject:content];
            
        }
        self.appList = downloaded;
        return;
    }
    else{
        self.URLapplist = [[jingtai sharedSingleton].URLappList mutableCopy];
    NSMutableArray * nodownload =[[NSMutableArray alloc] init];
    NSMutableArray * downloaded =[[NSMutableArray alloc] init];
    NSMutableArray * update = [[NSMutableArray alloc] init];
    NSFileManager * fm = [NSFileManager defaultManager];
    NSString * download =[MHFileTool getLocalFilePath:@"downloaded.plist"];
    if([fm fileExistsAtPath:download])
    {
        self.localapplist = [[NSMutableArray alloc] initWithContentsOfFile:download];
    }
    if(self.localapplist.count==0)
    {
        self.appList= self.URLapplist;
    }
    else
    {
        for (NSDictionary *temp in self.localapplist) {
            for(Model * temp2 in self.URLapplist)
            {
                int  n =[((M_content *)temp2) check:(M_content *)temp2 andtwo:(NSDictionary *)temp];
                switch (n)
                        {
                    case 0:
                                [nodownload addObject:temp2];
                        break;
                    case 1:
                            {
                                
                                M_content * content = [[M_content alloc] init];
                                [content setself:temp];
                                content.state =1;
                                [downloaded addObject:content];
                            }
                                break;
                    case 2:
                            {
                                ((M_content *)temp2).state=2;
                              [update addObject:temp2];
                            }
                                break;
                    default:
                        break;
                }
            }
        }
        [self.appList addObjectsFromArray:update];
        [self.appList addObjectsFromArray:downloaded];
        [self.appList addObjectsFromArray:nodownload];
    }
    }
}

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    NSLog(@" 速度 X: %f, Y:%f",velocity.x,velocity.y);

    targetContentOffset->x = self.scroview.contentOffset.x;
    targetContentOffset->y= self.scroview.contentOffset.y;
                       [self.scroview setContentOffset:self.scroview.contentOffset animated:NO];
                       if(velocity.x>0.5)
                       {
                           
                           CGFloat Width = self.scroview.frame.size.width;
                           int n =  ((int)self.scroview.contentOffset.x)/self.scroview.frame.size.width;
                           float pageF=  (self.scroview.contentOffset.x- (n*self.scroview.frame.size.width))/(float)self.scroview.frame.size.width;
                           
                           
                           [self.scroview setContentOffset:CGPointMake(self.scroview.frame.size.width*(n+1), 0) animated:YES];
                            self.PageCon.currentPage = n+1;
                       
                           
                       }
                       else if(velocity.x<-0.5)
                       {
                           
                           CGFloat Width = self.scroview.frame.size.width;
          
                           int n =  ((int)self.scroview.contentOffset.x)/self.scroview.frame.size.width;
                           float pageF=  (self.scroview.contentOffset.x- (n*self.scroview.frame.size.width))/(float)self.scroview.frame.size.width;
                           
                           if(n-1<0)
                           {
                           [self.scroview setContentOffset:CGPointMake(self.scroview.frame.size.width*(0), 0) animated:YES];
                                self.PageCon.currentPage = 0;
                           }
                           else
                           {
                               [self.scroview setContentOffset:CGPointMake(self.scroview.frame.size.width*(n), 0) animated:YES];
                               self.PageCon.currentPage = n;
                           }
                        
                       }
    else
    {
        NSLog(@"%f",self.scroview.contentOffset.x);
        BOOL flag =NO;
        CGFloat Width = self.scroview.frame.size.width;
        if(self.scroview.contentOffset.x>0)
        {
            self.scroview.decelerationRate =100;
            int n =  ((int)self.scroview.contentOffset.x)/self.scroview.frame.size.width;
            float pageF=  (self.scroview.contentOffset.x- (n*self.scroview.frame.size.width))/(float)self.scroview.frame.size.width;
            if(pageF>=0.5)
            {
                
                
                [self.scroview setContentOffset:CGPointMake(self.scroview.frame.size.width*(n+1), 0) animated:YES];
                self.PageCon.currentPage = n+1;
                
            }
            else
            {
                
                [self.scroview setContentOffset:CGPointMake(self.scroview.frame.size.width*(n), 0) animated:YES];
                
                 self.PageCon.currentPage = n;
            }
        }
        else
        {
            
            [self.scroview setContentOffset:CGPointMake(0, 0) animated:YES ];
            
            
        }
        

    }

    
    
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate)
    {

                    //       [self.scroview setContentOffset:self.scroview.contentOffset animated:NO];
                               
        

    }
    else{

    }
  }
-(void)loadscroll
{
    //[self updateapplist:isdelete];
    CGRect frame = self.scroview.frame;
    [self.scroview removeFromSuperview];
    self.scroview = [[UIScrollView alloc] initWithFrame:frame];
    [self.view addSubview:self.scroview];
    self.scroview.indicatorStyle=UIScrollViewIndicatorStyleWhite;
    self.scroview.delegate = self;
    int totalColumns = 3;
    
    
    CGFloat Y =0;
    CGFloat W = self.scroview.frame.size.width/3.0;
    CGFloat H = self.scroview.frame.size.height/4.0;
    CGFloat X = (self.scroview.frame.size.width - totalColumns * W) / (totalColumns + 1);
    for ( int index = 0; index <_appList.count ; index++) {
        NSLog(@"indes %d",index);
        int indexID=0;
        Model *model = _appList[index];
        if((index+1)/12>0)
        {
            if((index+1)%12 ==0)
            {
                indexID=11;
            }
            else
            {
                indexID =(index+1)-((index+1)/12)*12-1;
            }
        }
        else
        {
            indexID =index;
        }
        int row = indexID / totalColumns;
        int col = indexID % totalColumns;
        CGFloat viewX;
        if((index+1)%12 == 0)
        {
            viewX  = X + col * (W + X)+((index)/12)*self.scroview.frame.size.width;
        }
        else{
            viewX = X + col * (W + X)+((index+1)/12)*self.scroview.frame.size.width;
        }
        CGFloat viewY = 0 + row * (H + Y);
        
        
        MyView *view = [[MyView alloc] initWithFrame:CGRectMake(viewX, viewY, W, H) Model:model MyButtonBlock:^{
            
            //            _myLabel = [[MyLabel alloc] init];
            //
            //            [self.view addSubview:_myLabel];
            
            NSLog(@"block");
        }];
        
        view.model = model;
        [self.scroview addSubview:view];
        [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
        CGFloat maxY = CGRectGetMaxY(view.frame);
        int n = (index/12)+1;
        self.scroview.contentSize = CGSizeMake(n*self.scroview.frame.size.width, 0);
        
        
        
        //_scroview.contentSize =  CGSizeMake(0, 266);
        
    }
    if(self.appList.count == 0)
    {
        [jingtai setlocalcontentVersion:[jingtai sharedSingleton].contentversion];
        self.PageCon.numberOfPages =0;
    }
    else{
    int n = (self.appList.count-1)/12;
    self.PageCon.numberOfPages = n+1;
    self.PageCon.currentPage =0;
    [jingtai setlocalcontentVersion:[jingtai sharedSingleton].contentversion];
    [self.view bringSubviewToFront:self.scroview];
    }
}

-(void)loadscroll:(BOOL)isdelete
{
     [self updateapplist:isdelete];
    CGRect frame = self.scroview.frame;
    [self.scroview removeFromSuperview];
    self.scroview = [[UIScrollView alloc] initWithFrame:frame];
    [self.view addSubview:self.scroview];
    self.scroview.indicatorStyle=UIScrollViewIndicatorStyleWhite;
    self.scroview.delegate = self;
    int totalColumns = 3;
    
    
    CGFloat Y =0;
    CGFloat W = self.scroview.frame.size.width/3.0;
    CGFloat H = self.scroview.frame.size.height/4.0;
    CGFloat X = (self.scroview.frame.size.width - totalColumns * W) / (totalColumns + 1);
    for ( int index = 0; index <_appList.count ; index++) {
        NSLog(@"indes %d",index);
        int indexID=0;
        Model *model = _appList[index];
        if((index+1)/12>0)
        {
            if((index+1)%12 ==0)
            {
                indexID=11;
            }
            else
            {
                indexID =(index+1)-((index+1)/12)*12-1;
            }
        }
        else
        {
            indexID =index;
        }
        int row = indexID / totalColumns;
        int col = indexID % totalColumns;
        CGFloat viewX;
        if((index+1)%12 == 0)
        {
            viewX  = X + col * (W + X)+((index)/12)*self.scroview.frame.size.width;
        }
        else{
             viewX = X + col * (W + X)+((index+1)/12)*self.scroview.frame.size.width;
        }
        CGFloat viewY = 0 + row * (H + Y);
        
        
        MyView *view = [[MyView alloc] initWithFrame:CGRectMake(viewX, viewY, W, H) Model:model MyButtonBlock:^{
            
            //            _myLabel = [[MyLabel alloc] init];
            //
            //            [self.view addSubview:_myLabel];
            
            NSLog(@"block");
        }];
        
        view.model = model;
        [self.scroview addSubview:view];
        [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
        CGFloat maxY = CGRectGetMaxY(view.frame);
        int n = (index/12)+1;
        self.scroview.contentSize = CGSizeMake(n*self.scroview.frame.size.width, 0);
        
        
        
        //_scroview.contentSize =  CGSizeMake(0, 266);
        
    }
    if(self.appList.count == 0)
    {
        [jingtai setlocalcontentVersion:[jingtai sharedSingleton].contentversion];
        self.PageCon.numberOfPages =0;
    }
    else{
        int n = (self.appList.count-1)/12;
        self.PageCon.numberOfPages = n+1;
        self.PageCon.currentPage =0;
        [jingtai setlocalcontentVersion:[jingtai sharedSingleton].contentversion];
        [self.view bringSubviewToFront:self.scroview];
    }

}
-(void)guanzhu:(id)sender
{
    if(((UIButton*)sender).tag ==0)
    {
    
    if(self.userid ==nil)
    {
        self.userid = @"56710e6d7019c9437946d314";
    }
    if(![UMComSession sharedInstance].uid)
    {
        [[UMComAction action] performActionAfterLogin:nil viewController:self completion:^(NSArray *data, NSError *error) {
            if (!error) {
                UMComUserProfileRequest * UMComUserP = [[UMComUserProfileRequest alloc] initWithUid:self.userid sourceUid:nil];
                [UMComUserP fetchRequestFromCoreData:^(NSArray *data, NSError *error) {
                    if(error!= nil)
                    {
                        NSLog(@"error  %@",error.description);
                    }
                    NSLog(@"feeds is %@",data);
                    UMComUser * user =[data firstObject];
                    [UMComPushRequest followerWithUser:user isFollow:YES completion:^(NSError * error){
                        NSLog(@"你已经关注用户 %@",user.name);
                        [self isGuanzhuzhanghao];
                       
                    }];
                }];
            }
        }];
    }
    UMComUserProfileRequest * UMComUserP = [[UMComUserProfileRequest alloc] initWithUid:self.userid sourceUid:nil];
    [UMComUserP fetchRequestFromServer:^(NSArray *data, BOOL haveNextPage, NSError *error) {
        NSLog(@"feeds is %@",data);
        if(error != nil)
        {
            NSLog(@"error  %@",error.description);
        }
        UMComUser * user =[data firstObject];
        [UMComPushRequest followerWithUser:user isFollow:YES completion:^(NSError * error){
            NSLog(@"你已经关注用户 %@",user.name);
            [self isGuanzhuzhanghao];
           
        }];

    }];
    }
    else
    {
        
        if(self.userid ==nil)
        {
            self.userid = @"56710e6d7019c9437946d314";
        }
        if(![UMComSession sharedInstance].uid)
        {
            [[UMComAction action] performActionAfterLogin:nil viewController:self completion:^(NSArray *data, NSError *error) {
                if (!error) {
                    UMComUserProfileRequest * UMComUserP = [[UMComUserProfileRequest alloc] initWithUid:self.userid sourceUid:nil];
                    [UMComUserP fetchRequestFromCoreData:^(NSArray *data, NSError *error) {
                        if(error!= nil)
                        {
                            NSLog(@"error  %@",error.description);
                        }
                        NSLog(@"feeds is %@",data);
                        UMComUser * user =[data firstObject];
                        [UMComPushRequest followerWithUser:user isFollow:NO completion:^(NSError * error){
                            NSLog(@"你已经取消关注 %@",user.name);
                             self.isguanzhu = NO;
                            [self isGuanzhuzhanghao];
                           
                        }];
                    }];
                }
            }];
        }
        UMComUserProfileRequest * UMComUserP = [[UMComUserProfileRequest alloc] initWithUid:self.userid sourceUid:nil];
        [UMComUserP fetchRequestFromServer:^(NSArray *data, BOOL haveNextPage, NSError *error) {
            NSLog(@"feeds is %@",data);
            if(error != nil)
            {
                NSLog(@"error  %@",error.description);
            }
            UMComUser * user =[data firstObject];
            [UMComPushRequest followerWithUser:user isFollow:NO completion:^(NSError * error){
                NSLog(@"你已经取消关注 %@",user.name);
                 self.isguanzhu = NO;
                [self isGuanzhuzhanghao];
                           }];
            
        }];

    }

}
-(void)dianzan:(id)sender
{
    NSLog(@"点赞");
    if(![UMComSession sharedInstance].uid)
    {
        [[UMComAction action] performActionAfterLogin:nil viewController:self completion:^(NSArray *data, NSError *error) {
            if (!error) {
                NSString * url = [NSString stringWithFormat:@"%@/cardmennew/dianzan.aspx?kindid=%@&comid=%@&isdianzan=YES",[jingtai sharedSingleton].mainIP,self.kindID,[UMComSession sharedInstance].uid];

                NSString * str = [[[GetURLContent alloc] init] getisdianzan:url];
                if([str isEqualToString:@"Successdianzan"])
                {
                    NSLog(@"已经点赞");
                    [self.dianzanButton setTitle:@"已经点赞" forState:UIControlStateNormal];
                    [self.dianzanButton setEnabled:NO];
                }
                          }
        }];
    }
    else
    {
        NSString * url = [NSString stringWithFormat:@"%@/cardmennew/dianzan.aspx?kindid=%@&comid=%@&isdianzan=YES",[jingtai sharedSingleton].mainIP,self.kindID,[UMComSession sharedInstance].uid];
    
        NSString * str = [[[GetURLContent alloc] init] getisdianzan:url];
        if([str isEqualToString:@"Successdianzan"])
        {
            NSLog(@"已经点赞");
            [self.dianzanButton setTitle:@"已经点赞" forState:UIControlStateNormal];
            [self.dianzanButton setEnabled:NO];
        }
    }

}
@end
